let request = require('request');

function DoRequest(tOptions,pCallback){
  if(tOptions.method=="GET"){
    request.get(tOptions, function(err, response, body){
        if(err){
            if(pCallback){
              pCallback(err,body);
            }
        }else{
            if(pCallback){
              pCallback(null,body);
            }
        }
    });
  }
  else if(tOptions.method=="POST"){
      request.post(tOptions, function(err, response, body){
        if(err){
            if(pCallback){
              pCallback(err,body);
            }
        }else{
            if(pCallback){
              pCallback(err,body);
            }
        }
    });
  }
}


export default (req, res) => {
  let sURL = "https://data-server.now.sh/api/auth"
  let tOptions={
      url:sURL,
      method: 'GET',
      headers:{
          'Content-Type': 'application/json',
          'Accept':'application/json'     
      },
      body:JSON.stringify({}) 
  }
  DoRequest(tOptions,(err,tResp)=>{
    let tResult=JSON.parse(tResp);
    if(tResult.Ok){
      let sURL=tResult.URL;
      if(sURL){
        res.end(`<h1>Google Play auth</h1><a href=${sURL}>Click to authorize!</a>`);
      }
    }
  });
}